package com.tc.activiti.base.entity;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tc.activiti.base.constants.StateEnum;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务实体，统一返回格式
 */
public class APIResult<T> implements Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;
    /**
     * 状态码，成功值200，失败请查阅错误编码表
     */
    private int state = StateEnum.SUCCESS.getCode();
    /**
     * 请求路径
     */
    private String request;
    /**
     * 响应消息
     */
    private String message = "";
    /**
     * 错误原因
     */
    private String cause = "";
    /**
     * 响应变量
     */
    private Map<String, Object> variables = new HashMap<String, Object>();
    /**
     * 响应数据
     */
    private T data;

    public APIResult() {
    }

    public APIResult(int state) {
        super();
        this.state = state;
    }

    public APIResult(int state, String message) {
        this.state = state;
        this.message = message;
    }

    public APIResult(int state, String message, String cause) {
        this.state = state;
        this.message = message;
        this.cause = cause;
    }

    /**
     * 是否成功
     *
     * @return
     */
    @JsonIgnore
    public boolean isSuccess() {
        return StateEnum.SUCCESS.getCode() == state;
    }

    /**
     * 添加响应变量
     *
     * @param key   变量key
     * @param value 变量值
     */
    public void addVariable(String key, Object value) {
        this.variables.put(key, value);
    }

    /**
     * 获取变量key的值
     *
     * @param key 变量key
     * @return
     */
    public Object getVariable(String key) {
        if (null == variables) {
            return null;
        }
        return variables.get(key);
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCause() {
        return cause;
    }

    public void setCause(String cause) {
        this.cause = cause;
    }

    public Map<String, Object> getVariables() {
        return variables;
    }

    public void setVariables(Map<String, Object> vars) {
        this.variables = vars;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "APIResult{" +
                "state=" + state +
                ", request='" + request + '\'' +
                ", message='" + message + '\'' +
                ", cause='" + cause + '\'' +
                ", variables=" + variables +
                ", data=" + data +
                '}';
    }
}
